//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "server\fn_serverSetupConquer.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object"];
DEFAULT_PARAM(_object,TMF_REGISTER_OBJECT,objNull);

if (!isnull _object) then {
	_this spawn {
		private ["_object","_action","_parameters","_i","_conquer_msg","_conquer_zone","_conquer_owner","_shared_variable","_shared_variable_value","_conquer_code","_conquer_zone_center","_conquer_zone_size"
				,"_conquer_zone_angle","_conquer_zone_rect","_object_identifier","_conquer_left","_base_conquer_timeout","_conquer_timeout","_conquer_count","_conquer_taker","_count_owners","_count_takers"];
		DEFAULT_PARAM(_object,TMF_REGISTER_OBJECT,objNull);
		DEFAULT_PARAM(_action,TMF_REGISTER_ACTION,[]);
		DEFAULT_PARAM(_instance_id,TMF_REGISTER_INSTANCE,TMF_SERVER_INSTANCE_ID);
		_action_id = [_action] call FFUNC(actionGetID);
		_base_conquer_timeout = ["BASE_CONQUERED_TIMEOUT",30] call FFUNC(getFrameworkValue);
		TRACE_1("Action",_action);
		_parameters = [_action] call FFUNC(actionGetParams);
		_conquer_msg = [_parameters, "CONQUER_MSG", ""] call CFUNC(hashGet);
		_conquer_zone = [_parameters, "CONQUER_ZONE", [getPos _object]] call CFUNC(hashGet);
		_conquer_owner = [_parameters, "CONQUER_OWNER", GVAR(side_enemy)] call CFUNC(hashGet);
		_shared_variable = [_parameters, "SHARED_VAR", ""] call CFUNC(hashGet);
		_conquer_left = [_parameters, "CONQUER_LEFT", 3] call CFUNC(hashGet);
		_conquer_code = [_parameters, "CONQUER_CODE", ""] call CFUNC(hashGet);
		_conquer_timeout = [_parameters, "CONQUER_TIMEOUT", _base_conquer_timeout] call CFUNC(hashGet);
		_conquer_taker = [_parameters, "CONQUER_TAKER", GVAR(side_own)] call CFUNC(hashGet);

		if (typename _conquer_owner == typename []) then {
			for [{_i = 0},{_i < count _conquer_owner},{_i = _i + 1}] do {
				if (typename (_conquer_owner select _i) == typename "") then {
					_conquer_owner set [_i, (switch {toupper((_conquer_owner select _i))} do { case "EAST": {east}; case "WEST": {west}; case "GUER": {resistance};	case "CIV": {civilian};	default{ GVAR(side_enemy)};	})];
				};
			};
		} else {
			if (typename _conquer_owner == typename "") then {
				_conquer_owner = switch {toupper(_conquer_owner)} do { case "EAST": {east}; case "WEST": {west}; case "GUER": {resistance};	case "CIV": {civilian};	default{ GVAR(side_enemy)};	};
			};
			_conquer_owner = [_conquer_owner];
		};
		if (typename _conquer_taker == typename []) then {
			for [{_i = 0},{_i < count _conquer_taker},{_i = _i + 1}] do {
				if (typename (_conquer_taker select _i) == typename "") then {
					_conquer_taker set [_i, (switch {toupper((_conquer_taker select _i))} do { case "EAST": {east}; case "WEST": {west}; case "GUER": {resistance};	case "CIV": {civilian};	default{ GVAR(side_own)};	})];
				};
			};
		} else {
			if (typename _conquer_taker == typename "") then {
				_conquer_taker = switch {toupper(_conquer_taker)} do { case "EAST": {east}; case "WEST": {west}; case "GUER": {resistance};	case "CIV": {civilian};	default{ GVAR(side_own)};	};
			};
			_conquer_taker = [_conquer_taker];
		};
		
		if (typename _conquer_zone == typename "") then {
			_conquer_zone = [getMarkerPos _conquer_zone, getMarkerSize _conquer_zone, MarkerDir _conquer_zone, ((MarkerShape _conquer_zone) == "RECTANGLE")];
		};
		_conquer_zone_center = GETVAL(_conquer_zone,0,getPos _object);
		_conquer_zone_size = GETVAL(_conquer_zone,1,100);
		_conquer_zone_angle = GETVAL(_conquer_zone,2,0);
		_conquer_zone_rect = GETVAL(_conquer_zone,3,true);
		
		if (typename _conquer_zone_size == typename []) then {
			_conquer_zone_size = GETVAL(_conquer_zone_size,0,100);
		};
		if (!isNil "_shared_variable") then {
			if (_shared_variable != "") then {
				[_instance_id,_shared_variable,false] call FFUNC(setMissionJIPVar);
				[QGVAR(all_var), [_instance_id,_shared_variable,false],THIS_FILE_] call NFUNC(callEvent);
			};
		};
		_conquer_count = 0;
		_conquer_start = time;
		_count_owners = 0;
		_count_takers = 0;
		while { !(isnull _object) && (_conquer_count <= _conquer_timeout) } do {
			_entities = (_conquer_zone_center) nearEntities [["Man","LandVehicle"], _conquer_zone_size];
			
			_count_owners = 0;
			{ _count_owners = _count_owners + (_x countSide _entities);
			} foreach _conquer_owner;
			_count_takers = 0;
			{ _count_takers = _count_takers + (_x countSide _entities);
			} foreach _conquer_taker;
			
			TRACE_1("Conquer",ARR_4(count _entities, _count_owners, _count_takers, _conquer_count));
			if ((_count_owners <= _conquer_left && _count_takers > 0) || (_conquer_left < 0 && _count_owners < _count_takers)) then {
				_conquer_count = time - _conquer_start;
			} else {
				_conquer_start = time;
			};
			sleep 2.3456;
		};
		
		if (!(isnull _object) && _conquer_count > _conquer_timeout) then {
			TRACE_1("Conquered",ARR_2(_count_takers, _conquer_count));
			if (!isNil "_conquer_msg") then {
				if (count _conquer_msg > 0) then {[QGVAR(all_message),[_conquer_msg,"all",objNull,"hq"],THIS_FILE_] call NFUNC(callEvent);};
			};
			if (!isNil "_shared_variable") then {
				if (_shared_variable != "") then {
					[_instance_id,_shared_variable,alive _object] call FFUNC(setMissionJIPVar);
					[QGVAR(all_var), [_instance_id,_shared_variable,alive _object],THIS_FILE_] call NFUNC(callEvent);
				};
			};
			if (_conquer_code != "") then {
				[QGVAR(all_execute),[_conquer_code,_object,_action],THIS_FILE_] call NFUNC(callEvent);
			};			
		};
	};
};	
TRACE("End");
TMFPROFILERSTOP;